<template>
	<view class="page">
		<view class="top-withdrawals-info">
			<image class="bg-image" src="/static/images/img_mine.png"></image>
			<view class="page-navbar">
				<image @click="toReturn" src="/static/images/ic_black_back.png"></image>
				<text>提现</text>
			</view>
		</view>
		<view class="full-amount">
			<view class="title">可提现金额(元)</view>
			<view class="money">
				<text>￥</text>
				{{ balance }}
			</view>
		</view>
		<view class="input-amount">
			<view class="input-amount-title">可提现金额（元）</view>
			<view class="input-box">
				<text>￥</text>
				<input @blur="inputAmont" v-model="params.amount" placeholder="请输入提现金额" placeholder-style="font-size:28rpx" />
				<text class="all-withdrawal" @click="allWithdrawals">全部提现</text>
			</view>
			<view class="withdrawal-way">
				<view>提现至</view>
				<view class="way">微信余额</view>
			</view>
			<view class="withdrawal-btn" @click="subbmit">申请提现</view>
		</view>
	</view>
</template>

<script>
	import {
		escortBalanceWithdrawal,
		accountInfo,
		userBalanceWithdrawal,
		promotionAccount
	} from '@/apis/request.js';

	export default {
		data() {
			return {
				balance: 0, //余额,
				params: {
					amount: 0,
					subType: 2 // 1微信APP 2微信小程序支付 3微信H5支付
				},
				type: 0 //0陪诊师 1用户
			};
		},
		methods: {
			toReturn() {
				uni.navigateBack();
			},
			//陪诊师余额
			getAccountInfo() {
				accountInfo().then(res => {
					this.balance = res.balance;
				});
			},
			//用户余额
			getAccount() {
				promotionAccount().then(res => {
					this.balance = res.balance;
				});
			},
			inputAmont() {
				let money = this.params.amount * 1;
				if (isNaN(money)) {
					uni.showToast({
						title: '请输入正确金额',
						icon: 'none',
						duration: 2000
					});
					this.params.amount = 0;
				} else {
					if (money < 0) {
						uni.showToast({
							title: '小于提现最低金额',
							icon: 'none',
							duration: 2000
						});
						this.params.amount = 0;
					} else {
						this.params.amount = money;
					}
				}
			},
			//全部提现
			allWithdrawals() {
				this.params.amount = this.balance;
			},
			//提现按钮
			subbmit() {
				if (this.params.amount > this.balance) {
					uni.showToast({
						title: '提现金额不得大于余额！',
						icon: 'none',
						duration: 2000
					});
					return;
				}
				if (this.type == 1) {
					userBalanceWithdrawal(this.params).then(res => {
						uni.showToast({
							title: '提现成功',
							icon: 'success',
							duration: 2000
						});
						uni.$emit('popularizeMoneyChange');
						setTimeout(() => {
							uni.navigateBack();
						}, 1000);
					});
				} else {
					escortBalanceWithdrawal(this.params).then(res => {
						uni.showToast({
							title: '提现成功',
							icon: 'success',
							duration: 2000
						});
						uni.$emit('attendantMoneyChange');
						setTimeout(() => {
							uni.navigateBack();
						}, 1000);
					});
				}
			}
		},
		onLoad(options) {
			this.type = options.type;
			if (options.type == 1) {
				//用户
				this.getAccount();
			} else {
				//陪诊师
				this.getAccountInfo();
			}
		}
	};
</script>

<style scoped lang="scss">
	.page {
		background-color: #f2f2f2ff;
		height: 100%;
		min-height: 100vh;

		.top-withdrawals-info {
			height: 500rpx;
			width: 750rpx;

			.bg-image {
				height: 500rpx;
				width: 750rpx;
			}

			.page-navbar {
				text {
					position: absolute;
					top: 112rpx;
					left: 342rpx;
					font-size: 34rpx;
				}

				image {
					position: absolute;
					top: 112rpx;
					left: 30rpx;
					height: 34rpx;
					width: 18rpx;
				}
			}
		}

		.full-amount {
			position: absolute;
			left: 60rpx;
			top: 190rpx;

			.title {
				font-size: 26rpx;
				color: #333333;
			}

			.money {
				padding-top: 20rpx;
				font-size: 80rpx;
				font-weight: bold;

				text {
					font-size: 40rpx;
				}
			}
		}

		.input-amount {
			position: relative;
			margin: -120rpx auto 0 auto;
			padding: 40rpx;
			box-sizing: border-box;
			width: 690rpx;
			height: 410rpx;
			border-radius: 16rpx;
			background-color: #fff;

			.input-amount-title {}

			.input-box {
				border-bottom: 2rpx solid #efefef;
				padding-bottom: 24rpx;
				margin-top: 30rpx;
				display: flex;
				align-items: center;
				font-size: 40rpx;

				.all-withdrawal {
					text-align: right;
					width: 200rpx;
					font-size: 26rpx;
					color: #32C59A;
				}
			}

			.withdrawal-way {
				margin-top: 20rpx;
				display: flex;
				align-items: center;
				justify-content: space-between;
				font-size: 26rpx;

				.way {
					color: #999999;
				}
			}

			.withdrawal-btn {
				width: 610rpx;
				height: 88rpx;
				line-height: 88rpx;
				text-align: center;
				border-radius: 44rpx;
				background: #32C59A;
				margin: 50rpx auto 0 auto;
				color: #fff;
			}
		}
	}
</style>